package com.example;

import com.example.service.UserService;
import com.example.service.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component("myProcessor")
public class MyProcessor implements Processor{
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        // 代理的对象
        System.out.println(beanName+"=初始化后");
        if(beanName.equals("userService")) {
            Object proxyInstance = Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("代理");
//                    return method.invoke(bean, args);
                    return method.invoke(bean, args);
                }
            });
//            System.out.println("11111111111");
            return proxyInstance;
        }
        return bean;
    }
}
